/**
 * Copyright (C) 2008 aileron.cc
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package cc.aileron.web.openid;

import static cc.aileron.wsgi.WsgiHttpMethod.GET;
import static cc.aileron.wsgi.container.utility.WsgiProcessRegistry.registry;
import cc.aileron.web.openid.process.RequestProcess;
import cc.aileron.web.openid.process.VerifiedProcess;
import cc.aileron.web.openid.resources.OpenIdResource;
import cc.aileron.wsgi.WsgiController;
import cc.aileron.wsgi.WsgiProcess;
import cc.aileron.wsgi.container.WsgiContainer;
import cc.aileron.wsgi.context.WsgiApplicationContext;
import cc.aileron.wsgi.provider.WsgiLocalRedirectProvider;
import cc.aileron.wsgi.provider.WsgiRedirectProvider;

import com.google.inject.Inject;

/**
 * @author Aileron
 * 
 */
public class OpenIdController implements WsgiController
{
    /*
     * (非 Javadoc)
     * 
     * @see
     * cc.aileron.wsgi.WsgiSetting#setting(cc.aileron.wsgi.container.WsgiContainer
     * )
     */
    @Override
    public void setting(final WsgiContainer container)
    {
        /**
         * openid 認証要求
         */
        registry(OpenIdResource.class, "id", "returnUri").method(GET)
            .uri("/openid-auth")
            .process(requestProcess)
            .process(openIdProviderRedirect)
            .complete(container);

        /**
         * openid 認証の検証
         */
        registry(OpenIdResource.class).method(GET)
            .uri("/openid-auth")
            .overrideKey("verified")
            .process(verifiedProcess)
            .process(verifiedRedirect)
            .complete(container);
    }

    /**
     * constractor injection
     * 
     * @param context
     * @param redirectProvider
     * @param localRedirectProvider
     * @param requestProcess
     * @param verifiedProcess
     */
    @Inject
    public OpenIdController(
        final WsgiApplicationContext context,
        final WsgiRedirectProvider redirectProvider,
        final WsgiLocalRedirectProvider localRedirectProvider,
        final RequestProcess requestProcess,
        final VerifiedProcess verifiedProcess)
    {
        this.requestProcess = requestProcess;
        this.verifiedProcess = verifiedProcess;
        this.verifiedRedirect = localRedirectProvider.get("returnUri");
        this.openIdProviderRedirect = redirectProvider.get("openIdProviderUrl");
    }

    /**
     * open-id-provider への　redirect 要求
     */
    private final WsgiProcess<Object> openIdProviderRedirect;

    /**
     * redirect-provider
     */
    private final WsgiProcess<Object> verifiedRedirect;

    /**
     * requestProcess
     */
    private final RequestProcess requestProcess;

    /**
     * verifiedProcess
     */
    private final VerifiedProcess verifiedProcess;
}